<?php
class o2o_mdl_store_refuse_analysis extends dbeav_model{

    public function get_reason_analysis($filter=null){
        $sql = 'SELECT count(sra.reason_id) as num , rr.reason_name as name FROM sdb_o2o_store_refuse_analysis as sra right join sdb_o2o_refuse_reason as rr on sra.reason_id = rr.reason_id and '.$this->_filter($filter) .' GROUP BY rr.reason_id,rr.reason_name ORDER BY rr.reason_name';

        $row = $this->db->select($sql);
        return $row;
    }

    public function _filter($filter,$tableAlias=null,$baseWhere=null){
        $where = array(1);
        if(isset($filter['time_from']) && $filter['time_from']){
            $where[] = ' sra.createtime >='.strtotime($filter['time_from']);
        }

        if(isset($filter['time_to']) && $filter['time_to']){
            $where[] = ' sra.createtime <'.(strtotime($filter['time_to'])+86400);
        }

        if(isset($filter['type_id']) && $filter['type_id']){
            $where[] = ' sra.store_bn = \''.$filter['type_id'].'\'';
        }

        return parent::_filter($filter,'sra',$baseWhere)." AND ".implode($where,' AND ');
    }

}